package com.wang.leetcode;

/**
 * @Author: along
 * @Create: 2021/4/27
 * @Description:二叉搜索树范围和
 */
public class Solution938 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    int res;
    int l,h;
    public int rangeSumBST(TreeNode root, int low, int high) {
        l = low;
        h = high;
        if (root == null) {
            return 0;
        }
        process(root);
        return res;

    }
    public void process(TreeNode root) {
        if(root == null) {
            return;
        }
        process(root.left);
        if(root.val >= l && root.val <= h) {
            res += root.val;
        }
        process(root.right);
    }

}

